<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\NotificationResource;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NotificationController extends Controller
{
    public function index()
    {
        $user = auth_user();
        $result = $user->notifications()->paginate();
        $resource = NotificationResource::collection($result);
        return api(RET_OK, MSG_OK, $resource);
    }

    public function markAsRead($id)
    {
        $user = auth_user();
        $notification = $user->notifications()->findOrFail($id);
        if (!$notification->read_at) {
            $notification->markAsRead();
        }
        return api(RET_OK, MSG_OK, ['id' => $id]);
    }

    public function destroy($id)
    {
        $user = auth_user();
        $notification = $user->notifications()->findOrFail($id);
        $notification->delete();
        return api(RET_OK, MSG_OK, ['id' => $id]);
    }
}
